package com.visionblue.business.ship.page.register.config

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.input.getInputField
import com.afollestad.materialdialogs.input.input
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.repository.db.MeasureMenuItem
import com.visionblue.business.ship.util.MLog
import com.visionblue.business.ship.util.ext.setVisible
import com.visionblue.business.ship.util.toast
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_menu_config.*

/**
 * @author: xlh
 * @date: 2020-07-21 5:52 PM
 * @desc:
 */
class MenuConfigAdapter : RecyclerView.Adapter<MenuConfigAdapter.VH>() {

    private val mData = mutableListOf<MeasureMenuItem>()

    var onItemClickListener: OnItemClickListener? = null

    class VH(override val containerView: View) : RecyclerView.ViewHolder(containerView),
        LayoutContainer

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_menu_config, parent, false)
        return VH(itemView)
    }

    override fun getItemCount() = mData.size

    override fun onBindViewHolder(holder: VH, position: Int) {

        val menuItem = mData[position]

        holder.ivEdit.setVisible(!menuItem.alias.isNullOrEmpty())
        holder.tvAlias.setVisible(!menuItem.alias.isNullOrEmpty())
        /*holder.cbCheck.text = menuItem.name
        holder.tvAlias.text = "(${menuItem.alias})"*/

        holder.cbCheck.text = "(${menuItem.alias})"
        holder.tvAlias.text = menuItem.name

        holder.cbCheck.setOnCheckedChangeListener(null)
        holder.cbCheck.isChecked = menuItem.isChecked

        holder.cbCheck.setOnCheckedChangeListener { _, isChecked ->
            menuItem.isChecked = isChecked

            // 至少选中一个
            if (!isChecked && mData.none { it.isChecked }) {
                toast { "至少选中一个" }
                menuItem.isChecked = true
                notifyItemChanged(holder.adapterPosition)
                return@setOnCheckedChangeListener
            }

            // 流量计最多同时选择两个-修改成3个
            if (isChecked && mData.count { it.isFlow() && it.isChecked } > 3) {
                toast { "流量计最多同时选择三个" }
                menuItem.isChecked = false
                notifyItemChanged(holder.adapterPosition)
                return@setOnCheckedChangeListener
            }

            onItemClickListener?.onMenuUpdate(menuItem)
        }

        holder.ivEdit.setOnClickListener {
            showEditDialog(it, menuItem, position)
        }
    }

    private fun showEditDialog(
        view: View,
        menuItem: MeasureMenuItem, position: Int
    ) {
        MaterialDialog(view.context).show {
            input(maxLength = 5, prefill = menuItem.alias)
            positiveButton {
                val alias = getInputField().text.toString()
                if (alias.isEmpty()) {
                    toast { "输入为空" }
                    return@positiveButton
                }

                dismiss()
                menuItem.alias = alias
                onItemClickListener?.onMenuUpdate(menuItem)
                notifyItemChanged(position)
            }

            negativeButton {
                dismiss()
            }
        }
    }

    fun setData(measureMenuItems: List<MeasureMenuItem>?) {
        if (measureMenuItems != null) {
            mData.clear()
            mData.addAll(measureMenuItems)
            notifyDataSetChanged()
        }
    }

    interface OnItemClickListener {
        fun onMenuUpdate(measureMenuItem: MeasureMenuItem)
    }


}